package com.ice.day726.thread;

import lombok.extern.slf4j.Slf4j;

import static java.lang.Thread.sleep;

/**
 * @Author ice2020x
 * @Date 2022-07-29 5:20
 * @Description:
 */

@Slf4j
public class ThreadTest7 {
    static int r1 = 0;
    static int r2 = 0;
    static int r3 = 0;

    public static void main(String[] args) {
        try {
            method2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void method2() throws Exception{
        Thread t1 = new Thread(() -> {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r1 = 10;
        });

        Thread t2 = new Thread(() -> {
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r1 = 20;
        });

        log.debug("开始计算");
        t1.start();
        t2.start();
        long start = System.currentTimeMillis();
        log.debug("join begin");

        t1.join();
        log.debug("t1 join end");
        t2.join(1500);
        log.debug("t2 join end");
        long end = System.currentTimeMillis();
        log.debug("r1:{},r2:{},cost:{}",r1,r2,(end-start));

    }

    public static void method1() {
        log.debug("开始");
        Thread t1 = new Thread(() -> {
            log.debug("开始");
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("结束");
            r1 = 10;
        });

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("r的结果" + r1);
    }
}
